package xin.marcher.module.rights.api.impl;

import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.core.PageResult;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.rights.api.UserApi;
import xin.marcher.module.rights.domain.dto.UserDTO;
import xin.marcher.module.rights.domain.request.ModifyPasswordRequest;
import xin.marcher.module.rights.domain.request.QueryUserRequest;
import xin.marcher.module.rights.domain.request.RemoveUserRequest;
import xin.marcher.module.rights.domain.request.UserRequest;
import xin.marcher.module.rights.service.UserService;

/**
 *
 */
@Slf4j
@DubboService(version = "1.0.0", interfaceClass = UserApi.class, retries = 0)
public class UserApiImpl implements UserApi {

    @Autowired
    private UserService userService;

    @Override
    public JsonResult saveUser(UserRequest request) {
        try {
            return userService.saveUser(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult removeUser(RemoveUserRequest request) {
        try {
            userService.removeUser(request);
            return JsonResult.buildSuccess();
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult queryUser(QueryUserRequest request) {
        try {
            PageResult<UserDTO> pageResult = userService.queryUser(request);
            return JsonResult.buildSuccess(pageResult);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }

    @Override
    public JsonResult modifyPassword(ModifyPasswordRequest request) {
        try {
            return userService.modifyPassword(request);
        } catch (ProductBizException e) {
            log.error("biz error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getErrorCode(), e.getErrorMsg());
        } catch (Exception e) {
            log.error("system error: request={}", JSON.toJSONString(request), e);
            return JsonResult.buildError(e.getMessage());
        }
    }
}
